package com.neusoft.liduan.d0926.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.liduan.d0926.model.Admin;

/*	Dao类主要跟数据库打交道
 * 		负责数据库的增删改查
 * 	JavaBean
 * 		一般与数据库表结构一致，一般放在model或entity包中
 */
public class AdminDao extends BaseDao {
//	public boolean insert(Admin bean) {
//		Connection conn = getConn();
//		
//		//准备SQL命令
////		String sql = "insert into ums_admin values(" + bean.getId() + ")";
//		StringBuilder sb = new StringBuilder("insert into ums_admin values(");
////		sb.append("'");
////		sb.append(bean.getId());
////		sb.append("',");
//		sb.append("'");
//		sb.append(bean.getUsername());
//		sb.append("'");
////		sb.append("'");
////		sb.append(bean.getPassword());
////		sb.append("',");
////		sb.append("'");
////		sb.append(bean.getIcon());
////		sb.append("',");
////		sb.append("'");
////		sb.append(bean.getEmail());
////		sb.append("',");
////		sb.append("'");
////		sb.append(bean.getNickName());
////		sb.append("',");
////		sb.append("'");
////		sb.append(bean.getNote());
////		sb.append("',");
////		sb.append("'");
////		sb.append(bean.getCreateTime());
////		sb.append("',");
////		sb.append("'");
////		sb.append(bean.getLoginTime());
////		sb.append("',");
////		sb.append("'");
////		sb.append(bean.getStatus());
////		sb.append("'");
//		sb.append(")");
//		String sql = sb.toString();
//		System.out.println(sql);
//		Statement st = null;
//		try {
//			//通过数据库连接conn获取事务java.sql.Statement对象st
//			st = conn.createStatement();
//			//执行sql命令
//			int row = st.executeUpdate(sql);
//			
//			return row > 0;
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} finally {
//			close(null, st, conn);
//		}
//		
//		return false;
//	}

//	public boolean update(Admin bean) {
//		Connection conn = getConn();
//		//准备SQL命令
//		String sql = "update ums_admin set password = ?, status = ?, create_time = ? where id = ?";
//		System.out.println(sql);
//		PreparedStatement st = null;//可以防止sql注入攻击
//		try {
//			//通过数据库连接conn获取事务java.sql.Statement对象st
//			st = conn.prepareStatement(sql);
//			System.out.println(1);
//			st.setString(1, bean.getPassword());
////			st.setInt(parameterIndex, x);x不允许为null
//			st.setObject(2, bean.getStatus());
//			st.setString(3, bean.getCreateTime());
//			st.setLong(4, bean.getId());
//			System.out.println(2);
//			System.out.println(st.toString());
//			//执行sql命令
//			int row = st.executeUpdate();
//			
//			return row > 0;
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} finally {
//			close(null, st, conn);
//		}
//		
//		return false;
//	}

	public boolean insert(Admin bean) {
		String sql = "insert into ums_admin(id, username, password, icon, email, nick_name, note, create_time, login_time, status) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		return doIUD(sql, bean.getId(), bean.getUsername(), bean.getPassword(), bean.getIcon(), bean.getEmail(),
				bean.getNickName(), bean.getNote(), bean.getCreateTime(), bean.getLoginTime(), bean.getStatus());
	}

	public boolean update(Admin bean) {
		String sql = "update ums_admin set password = ?, status = ?, create_time = ? where id = ?";
		return doIUD(sql, bean.getPassword(), bean.getStatus(), bean.getCreateTime(), bean.getId());
	}

	public boolean delete(int id) {
		String sql = "delete from ums_admin where id = ?";
		return doIUD(sql, id);
	}
	
	/*	查询方法
	 * 		1、查询多条
	 * 		2、查询单条
	 */
	public Admin getById(Integer id) {
		Connection conn = getConn();
		String sql = "select * from ums_admin where id = ?";
		PreparedStatement st = null;//可以防止sql注入攻击
		ResultSet rs = null;
		Admin bean = null;
		try {
			//通过数据库连接conn获取事务java.sql.Statement对象st
			st = conn.prepareStatement(sql);
			st.setObject(1, id);
			//执行sql命令
			rs = st.executeQuery(sql);
			//处理查询结果集
//			while(rs.next()) {
//				bean = new Admin();
//				bean.setId(rs.getLong("id"));
//				bean.setUsername(rs.getString("username"));
//				bean.setPassword(rs.getString("password"));
//				//...
//				break;
//			}
			if(rs.next()) {
				bean = new Admin();
				bean.setId(rs.getLong("id"));
				bean.setUsername(rs.getString("username"));
				bean.setPassword(rs.getString("password"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(rs, st, conn);
		}
		
		return bean;
	}
	

	public List<Admin> getList() {
		Connection conn = getConn();
		String sql = "select * from ums_admin";
		PreparedStatement st = null;//可以防止sql注入攻击
		ResultSet rs = null;
		Admin bean = null;
		List<Admin> list = new ArrayList<Admin>();
		try {
			//通过数据库连接conn获取事务java.sql.Statement对象st
			st = conn.prepareStatement(sql);
			//执行sql命令
			rs = st.executeQuery(sql);
			//处理查询结果集
//			bean = new Admin();
			while(rs.next()) {
				bean = new Admin();
				bean.setId(rs.getLong("id"));
				bean.setUsername(rs.getString("username"));
				bean.setPassword(rs.getString("password"));
				//...
				list.add(bean);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(rs, st, conn);
		}
		
		return list;
	}
}
